<?php
/**
 * Created by PhpStorm.
 * User: xialei
 * Date: 16/4/13
 * Time: 20:30
 */
namespace app\components;

use yii\base\ErrorException;
use yii\base\Exception;
use yii\web\HttpException;

class ErrorHandler extends \yii\web\ErrorHandler
{
	protected function convertExceptionToArray($exception)
	{
		$data = [
			'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception',
			'message' => $exception->getMessage(),
			'code' => $exception->getCode(),
		];
		$return = [];
		$return['errmsg'] = $data['message'];
		$return['errcode'] = $data['code'] == 0 ? 1 : $data['code'];
		if ($exception instanceof HttpException)
		{
			$return['errcode'] = $exception->statusCode;
		}
		if ($exception instanceof \yii\db\Exception)
		{
			$return['errmsg'] = '加载失败';
			$return['errcode'] = 500;
		}
		if (YII_ENV_DEV)
		{
			$return['stack-trace'] = explode("\n", $exception->getTraceAsString());
		}
		if ($return['errcode'] == 403 && !$exception->getMessage())
		{
			$return['errmsg'] = '权限不足';
		}
		if ($return['errcode'] === 401)
		{
			$return['errmsg'] = $exception->getMessage();
		}
		return $return;
	}

}